﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using sauyou.Utils;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace sauyou.Admin
{
    public partial class yhlx_Add : ManagePage
    {

        /// <summary>
        /// 编号
        /// </summary>
        private string mDataId
        {
            get
            {
                object obj = (object)ViewState["mdataId"];
                if (obj != null)
                    return (string)obj;
                else
                    return "";
            }
            set { ViewState["mdataId"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                mDataId = Request["dataId"];

                if (!string.IsNullOrEmpty(mDataId))
                {
                    chkLoginLevel("edityhlx");
                    ShowInfo();
                }
                else
                {
                    chkLoginLevel("addyhlx");
                }

            }
        }

        private void ShowInfo()
        {
            using (SqlDataReader reader = DbHelperSQL.ExecuteReader("select * from user_yhlx where ID='" + mDataId + "'"))
            {
                if (reader.Read())
                {
                    name.Text = Convert.ToString(reader["name"]);
                    sortid.Text = Convert.ToString(reader["sortid"]);
                }
            }
        }

        public bool SaveData()
        {
            string sql = "";
            SqlParameter[] parameters = new SqlParameter[] { 
                    new SqlParameter("@name", SqlDbType.NVarChar,50),
                    new SqlParameter("@sortid", SqlDbType.Int,4),
                    new SqlParameter("@createtime", SqlDbType.DateTime,8,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")),
                    new SqlParameter("@createID", SqlDbType.NVarChar,50),
                    new SqlParameter("@Id",SqlDbType.Int,4)
            };

            parameters[0].Value = name.Text;
            parameters[1].Value = sortid.Text;

            if (!string.IsNullOrEmpty(mDataId))
            {
                sql = @"update user_yhlx set name=@name,sortid=@sortid where id=@id";//修改
                parameters[4].Value = mDataId;

                return DbHelperSQL.ExecuteSql(sql, parameters) > 0 ? true : false;
            }
            else
            {
                sql = @"insert into user_yhlx(name,sortid,createtime,createID) values (@name,@sortid,@createtime,@createID);select @@IDENTITY";//新增

                parameters[2].Value = DateTime.Now;
                parameters[3].Value = admin.UserName;
                object obj = DbHelperSQL.GetSingle(sql, parameters);
                if (obj == null)
                {
                    return false;
                }
                else {
                    mDataId = Convert.ToString(obj);
                    return true;
                }
                
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveData())
            {
                JscriptPrint("操作成功！", "yhlx_List.aspx", "Success");
                this.ShowInfo();
            }
            else
            {
                JscriptPrint("操作失败！", "yhlx_add.aspx", "Error");
            }

        }
    }
}
